<?php
class CommonAction extends Action{
	//首页
	public function index(){
		$this->validate();
		$actionName = $this->getActionName();
		$model = D($actionName);
		import("@.ORG.Page"); //导入分页类 
		$pk= $model->getPk();
		switch ($actionName){
			case "FeaturedSer":
			case "News":
			case "Careers":
			case "Seo":
			case "OfficeEnv":
			case "Hosting":
			 	if(!empty($_GET['type']))
		        {
		        	$count=$model->Table($model->typeParam['tableName'].' s')->join(' types t ON s.'.$model->typeParam['typeId'].' = t.type_id')->where("t.type_id='".$_GET['type']."' AND type_mid='".$model->typeParam['type_mid']."'")->count();
		        	$p = new Page ( $count, 10);
		        	$list=$model->Table($model->typeParam['tableName'].' s')->join(' types t ON s.'.$model->typeParam['typeId'].' = t.type_id')->where("t.type_id='".$_GET['type']."' AND type_mid='".$model->typeParam['type_mid']."'")->limit($p->firstRow.','.$p->listRows)->order('s.'.$pk.' asc')->findAll();
		        }else{
		        	$count=$model->Table($model->typeParam['tableName'].' s')->join(' types t ON s.'.$model->typeParam['typeId'].' = t.type_id')->where("type_mid='".$model->typeParam['type_mid']."'")->count();
		        	$p = new Page ( $count, 10);
		        	$list=$model->Table($model->typeParam['tableName'].' s')->join(' types t ON s.'.$model->typeParam['typeId'].' = t.type_id')->where("type_mid='".$model->typeParam['type_mid']."'")->limit($p->firstRow.','.$p->listRows)->order($pk.' asc')->findAll();
		        }
				break;
			default:
		        $count = $model->count();    //计算总数 
        		$p = new Page ( $count, 10); 
        		$list=$model->limit($p->firstRow.','.$p->listRows)->order($pk.' asc')->findAll(); 
        		break;
		}

        $p->setConfig('header','篇记录'); 
        $p->setConfig('prev',"<"); 
        $p->setConfig('next','>'); 
        $p->setConfig('first','<<'); 
        $p->setConfig('last','>>'); 
        $page = $p->show(); 
        $this->assign("nowpage",!empty($_GET["p"])?$_GET["p"]:1);
        $this->assign("page", $page); 
        $this->assign("list", $list); 
        $this->display('index'); 
	}
	
	//添加数据
	public function add() {
		$this->validate();
		$actionName = $this->getActionName();
		$model = D($actionName);
		$Types = M('Types');
		$pk = $Types->getPk();
		$list	= $Types->where("type_mid='".$model->typeParam['type_mid']."'")->findAll();
		if($list) {
			$this->assign('list',$list);
			$this->display('add');
		}else{
			$errmsg=$Types->getError();
			$this->assign('errmsg',$errmsg);
			$this->display('add');
		}
	}
	
	//插入数据
	public function insert(){
		$this->validate();
		$actionName = $this->getActionName();
		$model = D($actionName);
		$Types = M('Types');
		$pk = $Types->getPk();
		$list = $Types->where("type_mid='".$model->typeParam['type_mid']."'")->findAll();
		if($vo = $model->create()){
			if($model->add()){
				$this->redirect($actionName.'/index');
			}else{
				
				$errmsg = $model->getDbError();
			}
		}else{
			
			$errmsg = $model->getError();
		}		
		$this->assign('errmsg',$errmsg);
		$this->assign('list',$list);
		$this->assign('vo',$_POST);
		$this->display('add');
	}
	
	//删除指定记录
	public function delete() {
		$this->validate();
		$actionName = $this->getActionName();
		$model = D($actionName);
		$id = $_REQUEST['id'];
		$pk= $model->getPk();
		if (isset ($id)) {
			$condition = array ($pk => array ('in', explode ( ',', $id ) ) );
			if (false !== $model->where ( $condition )->delete ()) {
				$this->redirect($actionName.'/index');
			} else {
				$errmsg = $model->getDbError();
			}
		}else{
			$errmsg = $model->getError();
		}
		$this->assign('errmsg',$errmsg);
		$this->display('index');
	}
	
	// 编辑数据
	public function edit() {
		$this->validate();
		if(!empty($_GET['id'])) {
			$actionName = $this->getActionName();
			$model = D($actionName);
			$Types = M('Types');
			$pk = $model->getPk();
			$list	= $Types->where("type_mid='".$model->typeParam['type_mid']."'")->findAll();
			$vo	= $model->where($pk."=".$_GET['id'])->find();
			if($vo) {
				$this->assign('list',$list);
				$this->assign('vo',$vo);
				$this->display('edit');
			}else{
				$errmsg=$model->getError();
				$this->assign('errmsg',$errmsg);
				$this->display('edit');
			}
		}else{
			$errmsg='编辑项不存在！';
			$this->assign('errmsg',$errmsg);
			$this->display('edit');
		}
	}
	
//更新数据
	public function update(){
		$this->validate();
		$actionName = $this->getActionName();
		$model = D($actionName);
		$pk = $model->getPk();
		if($_POST[$pk]){
			if($vo = $model->create()){
				if($model->save()){
					$this->redirect($actionName.'/index');
				}else{
					$errmsg = $model->getDbError();
				}
			}else{
				$errmsg = $model->getError();
			}
		}else{
			$errmsg = '编号不能为空！';
		}
		$this->assign('errmsg',$errmsg);
		$this->assign('vo',$_POST);
		$this->display('edit');
	}
	
	public function validate(){
		if(empty($_SESSION['admin'])){
			$this->redirect('Public/login');
		}
	}
}